package com.shop.loan.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.model.Logistics;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.vo.OrderVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisLockUtil;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.rentaldata.RentalDataUtil;
import com.shop.loan.service.LogisticsService;
import com.shop.loan.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import xyz.erupt.jpa.dao.EruptDao;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class LogisticsServiceImpl implements LogisticsService {

    @Resource
    EruptDao eruptDao;

    @Resource
    RedisUtil redisUtil;

    @Resource
    RedisLockUtil redisLockUtil;

    @Resource
    RentalDataUtil rentalDataUtil;

    public ResResult get(OrderVo orderVo) {
        if (orderVo.getOrderId() == null) {
            return new ResResult(Code.PARAM_LACK);
        }
        Logistics dbLogistics = eruptDao.queryEntity(Logistics.class, "order_id = :orderId", new HashMap<String, Object>(1) {{
            this.put("orderId", orderVo.getOrderId());
        }});
        Logistics logistics = JSON.parseObject(JSON.toJSONString(dbLogistics), Logistics.class);
//        logistics.setUserOrder(null);
        if (!StringUtils.hasLength(logistics.getNumber())) {
            return new ResResult(Code.LOGISTICS_NULL);
        }
//        logistics.setLogisticsName(Util.getLogisticsName(logistics.getType()));
        return new ResResult(Code.SUCCESS, logistics);
    }


    /**
     * 定时更新物流状态
     */
    @Async
    public void updateLogisticsByIng() {
//        Jedis jedis = redisUtil.getJedis();
//        String lockKey = RedisCache.updateLogisticsByIngLock;
//        String lockValue = UUID.randomUUID().toString();
//        try {
//            if (!redisLockUtil.lock(jedis, lockKey, lockValue)) {
//                return;
//            }
//            List<OrderLogisticsByMybatis> orderLogisticsByMybatisList = orderLogisticsByMybatisMapper.selectByList(new OrderLogisticsByMybatis());
//            if (orderLogisticsByMybatisList == null || orderLogisticsByMybatisList.size() <= 0) {
//                return;
//            }
//            System.out.println(orderLogisticsByMybatisList.size());
//            for (OrderLogisticsByMybatis orderLogisticsByMybatis : orderLogisticsByMybatisList) {
//                if (!StringUtils.hasLength(orderLogisticsByMybatis.getNumber())) {
//                    continue;
//                }
//                if (orderLogisticsByMybatis.getStatus() != null && (orderLogisticsByMybatis.getStatus() == 3 || orderLogisticsByMybatis.getStatus() == 2
//                        || orderLogisticsByMybatis.getStatus() == 4 || orderLogisticsByMybatis.getStatus() == 6)) {
//                    continue;
//                }
//
//                if (orderLogisticsByMybatis.getDeliverTime() != null && DateUtil.between(orderLogisticsByMybatis.getDeliverTime(), new Date(), DateUnit.DAY) > 10) {
//                    continue;
//                }
//
//                String infoLog = rentalDataUtil.getLogistics(orderLogisticsByMybatis.getType(), orderLogisticsByMybatis.getNumber(), orderLogisticsByMybatis.getAddressPhone());
//                if (!StringUtils.hasLength(infoLog) || !JSON.isValidObject(infoLog)) {
//                    continue;
//                }
//                JSONObject infoJs = JSON.parseObject(infoLog);
//                JSONArray jsonArray = infoJs.getJSONArray("data");
//                if (jsonArray.size() > 0) {
//                    if (infoJs.getString("state") != null) {
//                        orderLogisticsByMybatis.setStatus(Integer.valueOf(infoJs.getString("state")));
//                        if ("3".trim().equals(infoJs.getString("state"))) {
//                            //签收-签收时间
//                            JSONObject jsonObject = jsonArray.getJSONObject(0);
//                            orderLogisticsByMybatis.setArriveConfirmTime(DateUtil.parse(jsonObject.getString("time")));
//
//                            /**
//                             * 订单状态修改为已收货
//                             */
//                            UserOrderByMybatis userOrderByMybatis = new UserOrderByMybatis();
//                            userOrderByMybatis.setId(orderLogisticsByMybatis.getOrderId());
//                            userOrderByMybatis.setLogisticsStatus(2);
//                            userOrderByMybatisMapper.updateByPrimaryKeySelective(userOrderByMybatis);
//
//                            //实物修改订单状态
//                            UserOrderByMybatis dbUserOrder = userOrderByMybatisMapper.selectOrderById(orderLogisticsByMybatis.getOrderId());
//                            if (dbUserOrder != null && dbUserOrder.getGoodsType() == 1) {
//                                orderUtil.logisticsSignature(dbUserOrder.getId());
//                            }
//                        }
//                    }
//                    //发货时间
//                    JSONObject jsonObject = jsonArray.getJSONObject(jsonArray.size() - 1);
//                    orderLogisticsByMybatis.setDeliverTime(DateUtil.parse(jsonObject.getString("time")));
//                    orderLogisticsByMybatis.setInfoLog(infoLog);
//                    orderLogisticsByMybatisMapper.updateByPrimaryKeyWithBLOBs(orderLogisticsByMybatis);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error(Util.GetErrorDetails(e));
//        } finally {
//            if (jedis != null) {
//                redisLockUtil.release(jedis, lockKey, lockValue);
//                jedis.close();
//            }
//        }
    }


}
